package ui;

/**
 * @Description 更新物流要求
 * @Author zengzerui
 * @Date 2025/5/2
 */

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import model.LogisticsRequirement;
import model.vo.QueryRequirementVO;
import service.LogisticsRequirementService;

public class UpdateRequirementPage {
    private Stage stage;
    private LogisticsRequirementService service;
    private QueryRequirementVO currentRequirement; // 新增字段保存当前编辑的对象

    public UpdateRequirementPage(Stage stage,QueryRequirementVO requirement) {
        this.stage = stage;
        this.service = new LogisticsRequirementService();
        this.currentRequirement = requirement;// 根据ID查询数据
    }

    public void show() {
        VBox root = new VBox(50);  // 增加间距
        root.setPadding(new Insets(60));  // 增加内边距
        root.setAlignment(Pos.CENTER);
        root.setStyle("-fx-background-color: white;");

        // 标题
        Label titleLabel = new Label("修改物流运输要求");
        titleLabel.setStyle("-fx-font-size: 32px; -fx-font-weight: bold;");  // 增大标题字体

        // 表单
        GridPane form = new GridPane();
        form.setHgap(30);  // 增加水平间距
        form.setVgap(30);  // 增加垂直间距
        form.setAlignment(Pos.CENTER);
        form.setPadding(new Insets(40));
        form.setStyle("-fx-background-color: #f8f9fa; -fx-background-radius: 15;");


        // 项目
        Label itemLabel = new Label("项目:");
        TextField itemField = new TextField();
        setupField(itemLabel, itemField);
        form.add(itemLabel, 0, 1);
        form.add(itemField, 1, 1);

        // 具体要求
        Label requirementLabel = new Label("具体要求:");
        TextArea requirementArea = new TextArea();
        setupTextArea(requirementLabel, requirementArea);
        form.add(requirementLabel, 0, 2);
        form.add(requirementArea, 1, 2);

        Label proEffiLable = new Label("时效:");
        TextField proEffiField = new TextField();
        setupField(proEffiLable, proEffiField);
        form.add(proEffiLable, 0, 3);
        form.add(proEffiField, 1, 3);


        // 责任人
        Label responsibleLabel = new Label("责任人:");
        TextField responsibleField = new TextField();
        setupField(responsibleLabel, responsibleField);
        form.add(responsibleLabel, 0, 4);
        form.add(responsibleField, 1, 4);

        // 监管人
        Label supervisorLabel = new Label("监管人:");
        TextField supervisorField = new TextField();
        setupField(supervisorLabel, supervisorField);
        form.add(supervisorLabel, 0, 5);
        form.add(supervisorField, 1, 5);


        //填充数据
        if (currentRequirement != null) {
            itemField.setText(currentRequirement.getItem());
            requirementArea.setText(currentRequirement.getRequirement());
            responsibleField.setText(currentRequirement.getResponsiblePerson());
            supervisorField.setText(currentRequirement.getSupervisor());
        }

        // 按钮容器
        HBox buttonBox = new HBox(30);  // 增加按钮间距
        buttonBox.setAlignment(Pos.CENTER);
        buttonBox.setPadding(new Insets(40, 0, 0, 0));



        // 保存按钮
        Button saveButton = new Button("保存");
        setupButton(saveButton, "button");
        saveButton.setOnAction(e -> {
            LogisticsRequirement requirement = new LogisticsRequirement(
                    currentRequirement.getSerial_no(),
                    itemField.getText(),
                    requirementArea.getText(),
                    proEffiField.getText(),
                    responsibleField.getText(),
                    supervisorField.getText()
            );
            service.updateRequirement(requirement);
            showAlert("保存成功", "物流运输要求已成功保存");
            RequirementListPage requirementListPage = new RequirementListPage(new Stage());
            try {
                requirementListPage.show();
                stage.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        // 返回按钮
        Button backButton = new Button("返回");
        setupButton(backButton, "back-button");
        backButton.setOnAction(e -> {
            RequirementListPage requirementListPage = new RequirementListPage(new Stage());
            try {
                requirementListPage.show();
                stage.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        buttonBox.getChildren().addAll(saveButton, backButton);

        root.getChildren().addAll(titleLabel, form, buttonBox);

        Scene scene = new Scene(root, 1300, 900);
        scene.getStylesheets().add(getClass().getResource("/css/logisticsList.css").toExternalForm());

        stage.setScene(scene);
        stage.setTitle("添加物流运输要求");
        stage.show();
    }

    private void setupField(Label label, TextField field) {
        // 设置标签样式
        label.setStyle("-fx-font-size: 18px;");  // 增大标签字体

        // 设置输入框样式
        field.setPrefWidth(500);  // 增加输入框宽度
        field.setPrefHeight(40);  // 增加输入框高度
        field.setStyle("-fx-font-size: 16px;");  // 设置输入框字体
    }

    private void setupTextArea(Label label, TextArea area) {
        // 设置标签样式
        label.setStyle("-fx-font-size: 18px;");

        // 设置文本区域样式
        area.setPrefWidth(500);
        area.setPrefHeight(120);  // 增加文本区域高度
        area.setWrapText(true);
        area.setStyle("-fx-font-size: 16px;");
    }

    private void setupButton(Button button, String styleClass) {
        button.getStyleClass().add(styleClass);
        button.setStyle("-fx-font-size: 16px; -fx-padding: 10 30;");  // 增大按钮字体和内边距
        button.setPrefWidth(150);  // 设置按钮宽度
        button.setPrefHeight(45);  // 设置按钮高度
    }

    private void showAlert(String title, String content) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        // 设置对话框字体
        DialogPane dialogPane = alert.getDialogPane();
        dialogPane.setStyle("-fx-font-size: 16px;");
        alert.showAndWait();
    }
}